<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@include file="../../common/operation_forbid.jsp" %>
<%@include file="../../common/jsVersion.jsp"%>
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
<script type="text/javascript">
	var returnUrl = '${returnUrl}';
    var contextPath = '${contextPath}';
    var documentType=${documentType};
    var paymentMethods=${paymentMethods};
    var contactTypes=${contactTypes};
	var haveReceivedPayDepositAmount=0;
    var customerId = '${customerId}';
    var roomId = '${roomId}';
    var userId = '${userId}';
    var payDepositId = '${payDeposit.id}';
    var subscribeId='${subscribe.id}';
    var subscribeCode="${subscribe.subscribeCode}";
    var agentId1="${subscribe.agentId}";
    var agentId2="${payDeposit.agentId}";
    var payDepositCode="${payDeposit.depositCode}";
    
    var menu = '${menu}';
    var signedInfoObj = JSON.parse('${signedInfo}');
    var extraInformationObj = JSON.parse('${extraInformation}');
    //认筹金额
    var payAepositAmount=0;
    //认购金额
    var subscribeAmount=0;
    //已收总金额
    var totalAmount = 0;
    var objectAmount;
    if('${signedContractAmount}' != ''){
    	objectAmount = JSON.parse('${signedContractAmount}');
    	totalAmount = objectAmount[0].total;
    }
    if(payDepositId!=""){
    	payAepositAmount=objectAmount[0].payAepositAmount;
    }
    if(subscribeId!=""){
    	payAepositAmount=objectAmount[0].payAepositAmount;
    	subscribeAmount=objectAmount[0].subscribeAmount;
    	payDepositCode="${subscribe.payDepositCode}";
    }
    if(payAepositAmount==""){
    	payAepositAmount=0;
    }
    if(subscribeAmount==""){
    	subscribeAmount=0;
    }
    //已收认购金
    haveReceivedPayDepositAmount=subscribeAmount;
    var totalHaveReceivedAmount=parseInt(subscribeAmount)+parseInt(payAepositAmount);
    var contractAmountName;
    if(${payDepositOff}){
    	contractAmountName="认购金:"+subscribeAmount+"合计:"+totalAmount;
    }else{
    	contractAmountName="认筹金:"+payAepositAmount+"认购金:"+subscribeAmount+"合计:"+totalAmount;
    }
     
    var customerProjectId='${customerProjectId}';
    var showChooseCustomer=${showChooseCustomer};
    var token = '${token}';
    
    //计算days天后的日期(时间，天数)
    function addDate(date,days){ 
    	var d=new Date(date); 
    	d.setDate(d.getDate()+days); 
    	return d; 
    }
    var receivable = addDate(new Date(), 30); //收款周期，默认30天
</script>
<!DOCTYPE html >
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>签约</title>
    <!-- neptune -->
    <%@include file="../../common/extInclude.jsp" %>
    <script type="text/javascript" src="${contextPath}/static/js/components/customerComponent.js?v=${jsVersion}"></script>
    <script type="text/javascript" src="${contextPath}/static/js/components/estateComponent.js?v=${jsVersion}"></script>
    <script type="text/javascript" src="${contextPath}/static/js/components/userComponent.js?v=${jsVersion}"></script>
    <script type="text/javascript" src="${contextPath}/static/js/customer/signed/edit.js?v=${jsVersion}"></script>
    <script type="text/javascript" src="${contextPath}/static/js/jquery/json2.js"></script> 
    <script type="text/javascript" src="${contextPath}/static/js/components/fileComponent.js?v=${jsVersion}"></script>
</head>
<script type="text/javascript">
var userIdStore = null;
var addPanel = null;
Ext.onReady(function() {
	Ext.Ajax.timeout = 180000;  
    Ext.define('Record', { 
        extend: 'Ext.data.Model',
        fields: ["relationAgentid","proportionCommission"]
    }); 
	
    userIdStore = new Ext.data.Store({
        fields: ["userName", "id"],
        proxy:{
            type:'ajax',
            url:contextPath+'/customer/customer/userByProjectIdForGroup',
            reader:{
                   type: 'json', 
                   root: 'root'  //数据
              }
        },
		autoLoad:false
    });
    userIdStore.load({
    	params:{
		  	'projectId': signedInfoObj.projectId
	  	}
    });
    
	Ext.tip.QuickTipManager.init()
	addPanel=Ext.create("customer.signed.addPanel");
	if(roomId!="" && null!=roomId){
		findRoom(roomId,null,null);
		Ext.getCmp("changeHouse").setVisible(false);
		//获取房间业态，以便获取收款周期
	    loadRoomType(roomId, signedInfoObj.projectId);
	}
	if(customerId!="" && null!=customerId){
		findCustomer(customerId);
		Ext.getCmp("changeCustomerBtn").setVisible(false);
	}
	
	//认购 转签约
	if(subscribeId!=""){
		 Ext.getCmp("changeHouse").setVisible(false);
		 Ext.getCmp("changeCustomerBtn").setVisible(false);
		//客户信息
		var cId = '${subscribe.customerId}';
		var rId = '${subscribe.roomId}';
		findCustomer(cId);
		//房间信息从认购信息中获取
		findRoom(rId,subscribeId,null);

	}
	//认筹转签约
	if(payDepositId!=""){
		//客户信息
		var cId = '${payDeposit.customerId}';
		findCustomer(cId);
		//房间信息
		var rId = '${payDeposit.roomId}';
		
		findRoom(rId,null,payDepositId);
		//认筹转认购，隐藏选择房间按钮,如果是无房认筹,就可以选择
		var isPaydeposit = '${payDeposit.isPaydeposit}';
		if(isPaydeposit=="0"){
			 Ext.getCmp("changeHouse").setVisible(false);
			 Ext.getCmp("changeCustomerBtn").setVisible(false);
		} 
	}
});


function findCustomer(customerId){
	//初始化房间信息
 	 Ext.Ajax.request({
	    url: contextPath+'/customer/customer/customerDetail',
	    params: {
	    	id: customerId
	    },
	    method: 'POST',
	    success: function(response){
	        var text = Ext.decode(response.responseText);
	        if(text.res=="1"){
	        	 //初始化客户信息
	     		  addPanel.getComponent("customerPanel").getForm().setValues(text.obj);
	     		  var familyMemberGridStore=addPanel.getComponent("customerPanel").getComponent("familyMemberGridPanel").getStore();
	     		  var extraParams=familyMemberGridStore.getProxy().extraParams;
	     		  if(payDepositId!="" || subscribeId!=""){
	     			  //当认筹ID不为空则加载认筹的客户成员信息
	     			   if(subscribeId!=""){
	     				 extraParams.buyId=subscribeId;
   		 	             extraParams.memberType='3';
	     			  }else if(payDepositId!=""){
	     				 extraParams.buyId=payDepositId;
   		 	             extraParams.memberType='2';
	     			  }
	     			  
	     		  }else{
	     			 extraParams.customerId=customerId;
	     			 extraParams.memberType='3';
	     		  }
	     		 extraParams.memberType='4';
 		 	     familyMemberGridStore.load();
	        }
	    }
	});
}

function findRoom(roomId,subId,payId){
	var reqUrl = "";
	if(subId!=null){
		reqUrl=contextPath+"/estate/projectRoom/findSubscribe?roomId="+roomId+"&subscribeId="+subId;
	}else if(payId!=null){
		reqUrl=contextPath+"/estate/projectRoom/findPayDeposit?roomId="+roomId+"&paydepositId="+payId;
	}else{
		reqUrl=contextPath+'/estate/projectRoom/roomDetail?id='+roomId;
	}
	//初始化房间信息
	 Ext.Ajax.request({
	    url:reqUrl ,
	    method: 'POST',
	    success: function(response){
	        var text = Ext.decode(response.responseText);
	        if(text.res=="1"){
	        	  addPanel.getComponent("estatePanel").getForm().setValues(text.obj);
	        	  
	        	  //设置折扣信息
	        		  //从房源信息里面获取
	        		  var result=text.obj;
	        		  
	        		  //折前单价  折前总价 折扣金额  折扣比例 折扣说明 签约单价 合同金额 认筹金额 认购金额 经济人
	        		  var values={};
	        		  values.discountAfterPrice = signedInfoObj.signAmount;
	        		  values.floorType=result.floorType;
	        		  //折前单价
	        		  values.calculateUnitPrice=result.calculateUnitPrice;
	        		  //折前总价 
	        		  values.totalPrice=result.totalPrice;
	        		  //折扣金额
	        		 
	        		  if(signedInfoObj.signAmount != null){
	        			  values.discountAfterAmount=result.totalPrice-signedInfoObj.signAmount;
	        		  }else{
	        			  values.discountAfterAmount=0;
	        		  }
	        		  //折扣比例
        			  values.discountRate=signedInfoObj.discountRate;
	        		  values.discountRateHidden=result.discountRate;
	        		  //签约单价
	        		  values.discountAmount=signedInfoObj.signPrice;
	        		  //合同金额=房款金额+签约款
	        		  values.discountTotalAmount=signedInfoObj.signAmount + signedInfoObj.decorationMoney;
	        		  //已收总金额
	        		  values.contractAmountName=contractAmountName;
	        		  //未收总金额=房款金额-已收金额+签约款
	        		  values.totalNeedReceivedAmount=signedInfoObj.signAmount-totalHaveReceivedAmount + signedInfoObj.decorationMoney;
 		 	          values.hetongPayPrice = totalHaveReceivedAmount;
 		 	          
 		 	          var subscribeInfoForm=Ext.getCmp("subscribeInfoForm").getForm();
 		 	          subscribeInfoForm.setValues(values);
 		 	          subscribeInfoForm.findField("isCalculate").setValue("1"); 
 		 	      		//0: 折后总价 1: 折扣比率
 		 	          if(result.floorType=="0"){
	        			  subscribeInfoForm.findField("discountAfterAmount").setReadOnly(true);
	        			  subscribeInfoForm.findField("discountRate").setReadOnly(true);
	        		  }else if(result.floorType=="1"){
	        			 subscribeInfoForm.findField("discountAfterPrice").setReadOnly(false);
	        			 subscribeInfoForm.findField("discountAfterAmount").setReadOnly(false);
	        			 //subscribeInfoForm.findField("discountAfterPrice").setVisible(false);
	        		  }
 		 	      	  //装修款
 		 	      	  if(Number(signedInfoObj.decorationMoney) > 0){
 		 	      			subscribeInfoForm.findField("decorationMoneyAmount").setValue(signedInfoObj.decorationMoney);
 		 	      			//选中装修款选项
	 		 	      		var item = subscribeInfoForm.findField("decorationMoney").items;
		        			item.each(function(row){
		        				row.setValue(true);
		        			});
 		 	      	  }else{
 		 	      			subscribeInfoForm.findField("decorationMoneyAmount").setValue(0);
 		 	      	  }
	        }
	    }
	});
	
	
}

</script>
<body>
</body>
</html>